var showName = document.getElementById("showName");
showName.innerHTML = "您好，" + sessionStorage.alias;

var showDay = document.getElementById("showDay");
var showDate = document.getElementById("showDate");

$(".exit").click(function () {
  $.ajax({
    type: "GET",
    url: "Logoutservlet",
    success: function (result) {
      sessionStorage.clear();
      var link = "login.html";
      window.location.href = link;
    }
  });
})


function timeAdd0(str) {
  if (str.length <= 1) {
    str = "0" + str
  }
  return str
}

setInterval(function () {
  var time = new Date(); // 程序计时的月从0开始取值后+1
  var m = timeAdd0((time.getMonth() + 1).toString());
  var day = time.getFullYear() + "-" + m + "-" +
    time.getDate() + " "
  var date = time.getHours() + ":" +
    time.getMinutes() + ":" + timeAdd0(time.getSeconds().toString());
  showDay.innerHTML = day;
  showDate.innerHTML = date;
}, 1000);

$("#changePasswordBtn").click(function () {

  var oldPassword = $('#oldPassword').val()
  var newPassword = $('#newPassword').val()
  var confirmPassword = $('#confirmPassword').val()
  if (oldPassword != "" && newPassword != "" && confirmPassword != "") {
    if (newPassword === confirmPassword) {
      $.ajax({
        type: "POST",
        url: "ChangePasswordServlet",
        dataType: "text",
        data: {
          username: sessionStorage.username,
          oldPassword: pidCrypt.MD5(oldPassword),
          newPassword: pidCrypt.MD5(newPassword),
          // confirmPassword: pidCrypt.MD5(confirmPassword)
        },
        success: function (result) {
          if (result == "false") {
            alert("密码修改失败");
          } else if (result == "ok") {
            alert("密码修改成功！");
          }else if (result == "dberror") {
              alert("数据库连接失败！");
          }else if (result == "olderror") {
              alert("原密码错误！");
          }else {
              alert("密码修改失败");
          }

        }
      });
    } else {
      alert("新密码与确认密码不一致！");
    }
  } else {
    alert("密码不能为空！");
  }


})
